package vip.xiaonuo.sys.modular.setting.columnssetting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.sys.modular.setting.columnssetting.entity.SysColumnsSetting;
import vip.xiaonuo.sys.modular.setting.columnssetting.mapper.SysColumnsSettingMapper;
import vip.xiaonuo.sys.modular.setting.columnssetting.param.SysColumnsSettingAddParam;
import vip.xiaonuo.sys.modular.setting.columnssetting.param.SysColumnsSettingEditParam;
import vip.xiaonuo.sys.modular.setting.columnssetting.param.SysColumnsSettingIdParam;
import vip.xiaonuo.sys.modular.setting.columnssetting.param.SysColumnsSettingPageParam;
import vip.xiaonuo.sys.modular.setting.columnssetting.service.SysColumnsSettingService;

import java.util.List;

/**
 * 列设置表Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/04/02 23:05
 **/
@Service
public class SysColumnsSettingServiceImpl extends ServiceImpl<SysColumnsSettingMapper, SysColumnsSetting> implements SysColumnsSettingService {

    @Override
    public Page<SysColumnsSetting> page(SysColumnsSettingPageParam sysColumnsSettingPageParam) {
        QueryWrapper<SysColumnsSetting> queryWrapper = new QueryWrapper<SysColumnsSetting>().checkSqlInjection();
        //数据归属
        queryWrapper.lambda().eq(SysColumnsSetting::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(sysColumnsSettingPageParam.getSortField(), sysColumnsSettingPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(sysColumnsSettingPageParam.getSortOrder());
            queryWrapper.orderBy(true, sysColumnsSettingPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(sysColumnsSettingPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SysColumnsSetting::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysColumnsSetting add(SysColumnsSettingAddParam sysColumnsSettingAddParam) {
        SysColumnsSetting sysColumnsSetting = BeanUtil.toBean(sysColumnsSettingAddParam, SysColumnsSetting.class);
        //数据归属
        sysColumnsSetting.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        sysColumnsSetting.setColumnsUserId(StpLoginUserUtil.getLoginUser().getId());
        this.save(sysColumnsSetting);
        return sysColumnsSetting;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysColumnsSetting edit(SysColumnsSettingEditParam sysColumnsSettingEditParam) {
        SysColumnsSetting sysColumnsSetting = this.queryEntity(sysColumnsSettingEditParam.getId());
        BeanUtil.copyProperties(sysColumnsSettingEditParam, sysColumnsSetting);
        sysColumnsSetting.setColumnsUserId(StpLoginUserUtil.getLoginUser().getId());
        this.updateById(sysColumnsSetting);
        return sysColumnsSetting;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SysColumnsSettingIdParam> sysColumnsSettingIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(sysColumnsSettingIdParamList, SysColumnsSettingIdParam::getId));
    }

    @Override
    public SysColumnsSetting detail(SysColumnsSetting sysColumnsSetting) {
        List<SysColumnsSetting> sysColumnsSettingList =  this.list(new LambdaQueryWrapper<SysColumnsSetting>().eq(SysColumnsSetting::getColumnsTableNo, sysColumnsSetting.getColumnsTableNo()).eq(SysColumnsSetting::getColumnsUserId,StpLoginUserUtil.getLoginUser().getId()).eq(SysColumnsSetting::getCompanyId,StpLoginUserUtil.getLoginUser().getCompanyId()));
        if(ObjectUtil.isNotEmpty(sysColumnsSettingList)) {
            return sysColumnsSettingList.get(0);
        }
        return null;
    }

    @Override
    public SysColumnsSetting queryEntity(String id) {
        SysColumnsSetting sysColumnsSetting = this.getById(id);
        if(ObjectUtil.isEmpty(sysColumnsSetting)) {
            throw new CommonException("列设置表不存在，id值为：{}", id);
        }
        return sysColumnsSetting;
    }
}
